package com.ruoyi.framework.web.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.entity.SysAdminUser;
import com.ruoyi.common.core.domain.entity.SysProject;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.entity.SysUserVO;
import com.ruoyi.system.service.ISysAdminProjectService;
import com.ruoyi.system.service.ISysAdminUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.UserStatus;
import com.ruoyi.common.exception.BaseException;
import com.ruoyi.system.service.ISysUserService;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 用户验证处理
 *
 * @author ruoyi
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService
{
    private static final Logger log = LoggerFactory.getLogger(UserDetailsServiceImpl.class);

    @Autowired
    private ISysUserService userService;
    @Autowired
    ISysAdminUserService adminUserService;
    @Autowired
    private ISysAdminProjectService projectService;

    @Override
    public UserDetails loadUserByUsername(String usernameStr) throws UsernameNotFoundException {
        String[] nameArr = usernameStr.split("\\|");
        String username = nameArr[0];
        int channel = Integer.valueOf(nameArr[1]);
        SysUserVO user = new SysUserVO();
        if(channel == 1){
            //运营端
            SysAdminUser adminUser = adminUserService.getOne(new LambdaQueryWrapper<SysAdminUser>()
                    .eq(SysAdminUser :: getAccount, username));
            if(Objects.isNull(adminUser)) {
                log.info("登录用户：{} 不存在.", username);
                throw new UsernameNotFoundException("用户：" + username + " 不存在");
            }
            BeanUtils.copyProperties(adminUser,user);
            user.setAdminFlag(true);
        }else {
            SysUser clintUser = userService.getOne(new LambdaQueryWrapper<SysUser>()
                    .eq(SysUser :: getPhone, username));
            if(Objects.isNull(clintUser)){
                //检查是否运营端账户
                SysAdminUser adminUser = adminUserService.getOne(new LambdaQueryWrapper<SysAdminUser>()
                        .eq(SysAdminUser :: getAccount, username));
                if(Objects.isNull(adminUser)) {
                    log.info("登录用户：{} 不存在.", username);
                    throw new UsernameNotFoundException("用户：" + username + " 不存在");
                }else {
                    BeanUtils.copyProperties(adminUser,user);
                    //查询用户所属项目
                    List<SysProject> projectList = projectService.getProjectByUser(user.getId());
                    user.setProjectList(projectList);
                    user.setAdminFlag(true);
                }
            }else {
                BeanUtils.copyProperties(clintUser, user);
                //查询用户所属项目
                SysProject project = projectService.getOne(new LambdaQueryWrapper<SysProject>()
                                .eq(SysProject :: getId, clintUser.getProjectId()));
                List<SysProject> projectList = new ArrayList<>();
                projectList.add(project);
                user.setProjectList(projectList);
                user.setAdminFlag(false);
            }
        }
        if (UserStatus.DELETED.getCode() == user.getDelFlag()) {
            log.info("登录用户：{} 已被删除.", username);
            throw new BaseException("对不起，您的账号：" + username + " 已被删除");
        } else if (UserStatus.DISABLE.getCode() == user.getStatus()) {
            log.info("登录用户：{} 已被停用.", username);
            throw new BaseException("对不起，您的账号：" + username + " 已停用");
        }
        user.setChannel(channel);
        return new LoginUser(user);
    }

}
